'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE, DECIMAL } = app.Sequelize;
  const OrderCompany = app.model.define(
      'OrderCompany',
      {
            id: {type: INTEGER, primaryKey: true},
            ship_id: INTEGER,
            cycle_id: INTEGER,
            count: INTEGER,
            status: STRING, // pending 进行中 confirmed 已确认
            pay_way: STRING(255), // 支付方式 paypal ali
            pay_url: TEXT,
            payment_status:STRING(255), // paid
            total_price: DECIMAL(10, 2), // 总价
            discount_amount: DECIMAL(10, 2),
            coupon_id: INTEGER,
            amount: DECIMAL(10, 2), // 实付款
            user_id: INTEGER,
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updated_time: {
              type: DATE,
              get() {
                return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
              }
            },
            order_no: STRING(255),
            pay_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('pay_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            status_value: STRING(255),
            status_name: STRING(255),
            // 联系人
            contact_name: STRING(255),
            contact_phone: STRING(255),
            contact_email: STRING(255),
            contact_wx: STRING(255),
            contact_remark: STRING(255),
      },
      {
          tableName: 'order_company',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
  OrderCompany.associate = function() {
        // 关联ship表
        app.model.OrderCompany.belongsTo(app.model.Ship, { foreignKey: 'ship_id', targetKey: 'id', as: 'ship' });
        // 关联cycle表
        app.model.OrderCompany.belongsTo(app.model.Cycle, { foreignKey: 'cycle_id', targetKey: 'id', as: 'cycle' });
        // 关联coupon表
        app.model.OrderCompany.belongsTo(app.model.Coupon, { foreignKey: 'coupon_id', targetKey: 'id', as: 'coupon' });
        // 用户
        app.model.OrderCompany.belongsTo(app.model.User, { foreignKey: 'user_id', targetKey: 'id', as: 'user' });
        // 一对多orderUser
        app.model.OrderCompany.hasMany(app.model.OrderUser, { foreignKey: 'order_id', targetKey: 'id', as: 'userList' });
    }
  return OrderCompany
};
// 生成mysql建表语句
//
